package com.ymatou.iapi.dguser.testcase;

import com.ymatou.iapi.dguser.parameter.GetUserListBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.parameter.UserDto;
import com.ymatou.iapi.dguser.service.GetUserListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.database.model.DGUserInfo;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Arrays;
import java.util.List;

/**
*获取用户列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserList{
	private static GetUserListBean getuserlistBean;
	private static GetUserListCall getuserlistCall;
	DGUserWapper DGUserWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户列表");
	}

	@Before
	public void caseUp() {
		getuserlistBean=new GetUserListBean();
		getuserlistCall=new GetUserListCall();
		DGUserWapper=new DGUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getuserlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(GetUserListBean getUserListBean) {
		List<UserDto> userDtos = getuserlistCall.getUserDtos();
		for (int userId:getUserListBean.getUserIds()
			 ) {
			DGUserInfo dgUserInfo = DGUserWapper.selectDGUserInfo(userId).get(0);
			userDtos.forEach(p->{
				if (p.getUserId() == userId){
					Logger.verifyEquals(p.getMobile(),dgUserInfo.getMobile(),"验证mobile");
					Logger.verifyEquals(p.getMobileAreaCode(),dgUserInfo.getMobileAreaCode(),"验证mobileAreaCode");
					Logger.verifyEquals(p.getNickname(),dgUserInfo.getNickname(),"验证nickname");
					Logger.verifyEquals(p.getLogoUrl(),dgUserInfo.getLogoUrl(),"验证logoUrl");
					Logger.verifyEquals(p.getGender(),dgUserInfo.getUserGender(),"验证gender");
					Logger.verifyEquals(p.getUserType(),dgUserInfo.getUserType(),"验证userType");
					Logger.verifyEquals(p.getUserStatus(),dgUserInfo.getUserStatus(),"验证useStatus");

				}
			});
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserList_001() {
		Logger.start(true,"获取用户列表");
		try {
			UserCreateBean u1 = DGUserCallService.initUsercreateBean();
			u1.setMobile(TestDataManager.getRandomNum(11));
			u1.setMobileAreaCode(TestDataManager.getRandomNum(6));
			UserCreateBean u2 = DGUserCallService.initUsercreateBean();
			u2.setMobile(TestDataManager.getRandomNum(11));
			u2.setMobileAreaCode(TestDataManager.getRandomNum(6));
			UserCreateBean u3 = DGUserCallService.initUsercreateBean();
			u3.setMobile(TestDataManager.getRandomNum(11));
			u3.setMobileAreaCode(TestDataManager.getRandomNum(6));
			UserCreateBean u4 = DGUserCallService.initUsercreateBean();
			u4.setMobile(TestDataManager.getRandomNum(11));
			u4.setMobileAreaCode(TestDataManager.getRandomNum(6));
			int user1 = DGUserCallService.UserCreateCall(u1);
			int user2 = DGUserCallService.UserCreateCall(u2);
			int user3 = DGUserCallService.UserCreateCall(u3);
			int user4 = DGUserCallService.UserCreateCall(u4);
			getuserlistBean.setUserIds(Arrays.asList(user1,user2,user3,user4));
			getuserlistCall.setData(getuserlistBean);
			getuserlistCall.callService();
			Logger.verifyEquals("true", getuserlistCall.getString("success"), "验证success");
			verifyDB(getuserlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserList_002() {
		Logger.start(true,"不输入userlist获取用户列表空");
		try {
			getuserlistCall.setData(getuserlistBean);
			getuserlistCall.callService();
			Logger.verifyEquals("true", getuserlistCall.getString("success"), "验证success");
			List<UserDto> userDtos = getuserlistCall.getUserDtos();
			Logger.verifyEquals(0,userDtos.size(), "验证userDtos的size");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserList_003() {
		Logger.start(true,"增加了loginId返回值");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			int user = DGUserCallService.UserCreateCall(u);
			getuserlistBean.setUserIds(Arrays.asList(user));
			getuserlistCall.setData(getuserlistBean);
			getuserlistCall.callService();
			Logger.verifyEquals("true", getuserlistCall.getString("success"), "验证success");
			List<UserDto> userDtos = getuserlistCall.getUserDtos();
			Logger.verifyEquals(1,userDtos.size(), "验证userDtos的size");
			Logger.verifyNotEquals("", getuserlistCall.getJsonObject().getAsJsonArray("userDtos").get(0).getAsJsonObject().get("loginId").getAsString(), "验证不为空");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserList_004() {
		Logger.start(true,"增加了shopid返回值");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setShopId(1);
			int user = DGUserCallService.UserCreateCall(u);
			getuserlistBean.setUserIds(Arrays.asList(user));
			getuserlistCall.setData(getuserlistBean);
			getuserlistCall.callService();
			Logger.verifyEquals("true", getuserlistCall.getString("success"), "验证success");
			List<UserDto> userDtos = getuserlistCall.getUserDtos();
			Logger.verifyEquals(1,userDtos.size(), "验证userDtos的size");
			Logger.verifyNotEquals(u.getShopId(), getuserlistCall.getJsonObject().getAsJsonArray("userDtos").get(0).getAsJsonObject().get("shopId").getAsString(), "验证不为空");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	
}